// Copyright (c) XiGuan 2023
// Author： xiguan
// Email: xiguan.teng@qq.com
// Create on 2023/11/20
// TODO:
//

#ifndef WEBSERVER_HTTP_HTTPRESPONSE_H
#define WEBSERVER_HTTP_HTTPRESPONSE_H

#include <Buffer.h>
#include <Log.h>
#include <sys/mman.h>
#include <sys/stat.h>

#include <unordered_map>

class HttpResponse {
 private:
  int code_;
  bool is_keep_alive_;

  std::string path_;
  std::string srcdir_;

  char *mm_file_;
  struct stat mm_file_stat_;

  static const std::unordered_map<std::string, std::string> SUFFIX_TYPE;
  static const std::unordered_map<int, std::string> CODE_STATUS;
  static const std::unordered_map<int, std::string> CODE_PATH;

 public:
  HttpResponse();
  ~HttpResponse();

  int getCode() const;
  char *getMmFile() const;

  void init(const std::string &srcdir, std::string &path,
            bool is_keep_alive = false, int code = -1);

  void make_response(Buffer &buffer);

  void unmap_file();

  void error_content(Buffer &buffer, std::string message);
  size_t file_len() const;

 private:
  void add_state_line_(Buffer &buffer);

  void add_header_(Buffer &buffer);

  void add_content_(Buffer &buffer);

  void error_html_();

  std::string get_file_type_();
};

#endif  // WEBSERVER_HTTP_HTTPRESPONSE_H
